package com.nineclock.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.sms.AliyunSmsUtils;
import com.nineclock.common.utils.PhoneNumCheckUtils;
import com.nineclock.system.service.SmsService;
import com.nineclock.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


/**
 * 验证码校验
 */
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    RedisTemplate<String, String> redisTemplate;
    @Autowired
    AliyunSmsUtils aliyunSmsUtils;

    @Override
    public void sendSmsCode(String mobile, String type) {
        //参数合法性校验
        //1.1 参数是否为空
        if (StrUtil.isEmpty(mobile) || StrUtil.isEmpty(type)){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //1.2. 手机号 合法性
        if ( !PhoneNumCheckUtils.isChinaPhoneLegal(mobile)){
            throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
        }
        //1.3. 如果是注册, 判断手机号是否存在, 如果存在,则不能注册
        if ("2".equals(type)){
            Boolean flag = sysUserService.checkExists(mobile);
            if (flag){
                throw new NcException(ResponseEnum.USER_MOBILE_EXISTS);
            }
        }else{
            Boolean flag = sysUserService.checkExists(mobile);
            if (!flag){
                throw new NcException(ResponseEnum.USER_NOT_REGISTER);
            }
        }

        //2. 生成验证码
        String randomNumbers = RandomUtil.randomNumbers(6);

        //3. 验证码存入redis (15分钟有效)
        String key = getPrefix(type) + mobile;
        redisTemplate.boundValueOps(key).set(randomNumbers,15,TimeUnit.MINUTES);

        //4. 验证码发给移动端
      /*  Boolean aBoolean = aliyunSmsUtils.sendSMS(mobile, randomNumbers);
        if (!aBoolean){
            throw new NcException(ResponseEnum.SEND_MESSAGE_ERROR);
        }*/

        log.info("短信发送成功, 验证码:  " + randomNumbers);

    }

    @Override
    public boolean verifySmsCode(String mobile, String type, String checkcode) {
        if (StrUtil.isEmpty(mobile) || StrUtil.isEmpty(type) || StrUtil.isEmpty(checkcode)){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        //redis中获取 验证码
        //获取前缀
        String prefix = getPrefix(type);
        String redisCode = redisTemplate.boundValueOps(prefix + mobile).get();

        return StrUtil.isNotEmpty(redisCode) && redisCode.equals(checkcode);
    }

    @Override
    public void deleteSmsCode(String mobile, String type) {
        String redisKey = this.getPrefix(type)+mobile;
        redisTemplate.delete(redisKey);
    }

    //redis 存储的key为 前缀 + 手机号
    //获取前缀
    public String getPrefix(String type){
        switch (type){
            case SMSConstant.SMS_TYPE_LOGIN:
                return SMSConstant.SMS_LOGIN_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_REGISTER:
                return SMSConstant.SMS_REGISTER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_CHANGE_MANAGER:
                return SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_RESET_PASSWORD:
                return SMSConstant.SMS_RESET_KEY_PREFIX;
            default:
                return null;
        }
    }
}
